package org.example.compare.schedule;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.example.compare.dto.*;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.file.Files;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 测试数据生成器类，用于为calculateItem_7函数生成测试数据
 * 该函数使用到的非函数参数列表外部变量包括:
 * 1. SchData.orderId2Order - 订单映射表
 * 2. SchData.processUrgentLevels - 工序加急等级映射
 * 3. hProcessSet - 需要进行排产的工序集
 * 4. sPositionsScore - 可以排产的工位
 * 5. hScoreByProcess - 按工序查找的分数表
 */
public class CalculateItem7TestDataGenerator {

    private static final ObjectMapper objectMapper = new ObjectMapper();
    private static final Random random = new Random();

    static {
        objectMapper.registerModule(new JavaTimeModule());
        objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * 生成随机测试数据并保存到文件
     */
    public static void generateRandomTestData(int iteration) throws IOException {
        String basePath = "../../test_data/calculateItem_7/random_case_" + iteration;
        File baseDir = new File(basePath);

        // 清空文件夹
        clearDirectory(baseDir);

        if (!baseDir.exists()) {
            baseDir.mkdirs();
        }

        // 生成测试数据
        Map<String, OrderDto> orderId2Order = generateOrderId2OrderData(basePath);
        generateProcessUrgentLevelsData(basePath);
        Map<Long, OrderProcess> hProcessSet = generateProcessSetData(basePath, orderId2Order);
        Set<Long> sPositionsScore = generatePositionScoreData(basePath);
        generateScoreByProcessData(basePath, hProcessSet, sPositionsScore);
        generateFunctionParams(basePath);
    }

    /**
     * 生成手动测试数据并保存到文件
     */
    public static void generateManualTestData(int caseNumber) throws IOException {
        String basePath = "../../test_data/calculateItem_7/manual_case_" + caseNumber;
        File baseDir = new File(basePath);

        // 清空文件夹
        clearDirectory(baseDir);

        if (!baseDir.exists()) {
            baseDir.mkdirs();
        }

        if (caseNumber == 1) {
            generateManualTestCase1(basePath);
        } else if (caseNumber == 2) {
            generateManualTestCase2(basePath);
        }
    }

    /**
     * 生成手动测试案例1 - 基本正常情况
     */
    private static void generateManualTestCase1(String basePath) throws IOException {
        // 生成订单数据
        Map<String, OrderDto> orderId2Order = new HashMap<>();

        // 订单1 (普通订单)
        OrderDto order1 = OrderDto.InitOrder();
        order1.setMLocalOrderNum("ORDER001");
        order1.setMColor("红色");
        order1.setMLineColorNum("LINE001");
        order1.setMOuterOrderNum("OUTER001");
        order1.mbPriFlag = false; // 非加急订单
        orderId2Order.put(order1.getMLocalOrderNum(), order1);

        // 订单2 (加急订单)
        OrderDto order2 = OrderDto.InitOrder();
        order2.setMLocalOrderNum("ORDER002");
        order2.setMColor("蓝色");
        order2.setMLineColorNum("LINE002");
        order2.setMOuterOrderNum("OUTER002");
        order2.mbPriFlag = true; // 加急订单
        orderId2Order.put(order2.getMLocalOrderNum(), order2);

        // 保存到文件
        objectMapper.writeValue(new File(basePath + "/orderId2Order.json"), orderId2Order);

        // 生成工序加急等级数据
        Map<String, Float> processUrgentLevels = new HashMap<>();
        processUrgentLevels.put("URGENT_A", 50.0f);
        processUrgentLevels.put("URGENT_B", 30.0f);
        processUrgentLevels.put("URGENT_C", 20.0f);
        objectMapper.writeValue(new File(basePath + "/processUrgentLevels.json"), processUrgentLevels);

        // 生成工序集数据
        Map<Long, OrderProcess> hProcessSet = new HashMap<>();

        // 工序1 (普通工序，属于订单1)
        OrderProcess process1 = OrderProcess.InitOrderProcess(order1);
        process1.setProcessId(1001L);
        process1.setOrderProcessId(1001L);

        ProcessSch scheduling1 = new ProcessSch();
        scheduling1.setMbHoldFlag(false);
        scheduling1.setMbNGFlag(false);
        scheduling1.setMbUrgentFlag(false); // 非加急工序
        scheduling1.setMbUrgentCode(""); // 无加急代码

        // 设置可做工位
        Set<Long> posCanDo1 = new HashSet<>();
        posCanDo1.add(10001L);
        posCanDo1.add(10002L);
        scheduling1.setMPosCanDo(posCanDo1);

        process1.setMScheduling(scheduling1);
        hProcessSet.put(1001L, process1);

        // 工序2 (加急工序，属于订单2)
        OrderProcess process2 = OrderProcess.InitOrderProcess(order2);
        process2.setProcessId(1002L);
        process2.setOrderProcessId(1002L);

        ProcessSch scheduling2 = new ProcessSch();
        scheduling2.setMbHoldFlag(false);
        scheduling2.setMbNGFlag(false);
        scheduling2.setMbUrgentFlag(true); // 加急工序
        scheduling2.setMbUrgentCode("URGENT_A"); // 加急代码A

        // 设置可做工位
        Set<Long> posCanDo2 = new HashSet<>();
        posCanDo2.add(10002L);
        posCanDo2.add(10003L);
        scheduling2.setMPosCanDo(posCanDo2);

        process2.setMScheduling(scheduling2);
        hProcessSet.put(1002L, process2);

        // 工序3 (挂起状态工序)
        OrderProcess process3 = OrderProcess.InitOrderProcess(order1);
        process3.setProcessId(1003L);
        process3.setOrderProcessId(1003L);

        ProcessSch scheduling3 = new ProcessSch();
        scheduling3.setMbHoldFlag(true); // 挂起状态
        scheduling3.setMbNGFlag(false);
        scheduling3.setMbUrgentFlag(false);
        scheduling3.setMbUrgentCode("");

        // 设置可做工位
        Set<Long> posCanDo3 = new HashSet<>();
        posCanDo3.add(10004L);
        scheduling3.setMPosCanDo(posCanDo3);

        process3.setMScheduling(scheduling3);
        hProcessSet.put(1003L, process3);

        // 工序4 (NG状态工序)
        OrderProcess process4 = OrderProcess.InitOrderProcess(order2);
        process4.setProcessId(1004L);
        process4.setOrderProcessId(1004L);

        ProcessSch scheduling4 = new ProcessSch();
        scheduling4.setMbHoldFlag(false);
        scheduling4.setMbNGFlag(true); // NG状态
        scheduling4.setMbUrgentFlag(true);
        scheduling4.setMbUrgentCode("URGENT_B");

        // 设置可做工位
        Set<Long> posCanDo4 = new HashSet<>();
        posCanDo4.add(10005L);
        scheduling4.setMPosCanDo(posCanDo4);

        process4.setMScheduling(scheduling4);
        hProcessSet.put(1004L, process4);

        // 保存到文件
        objectMapper.writeValue(new File(basePath + "/hProcessSet.json"), hProcessSet);

        // 生成可以排产的工位数据
        Set<Long> sPositionsScore = new HashSet<>();
        sPositionsScore.add(10001L);
        sPositionsScore.add(10002L);
        sPositionsScore.add(10003L);
        sPositionsScore.add(10004L);
        sPositionsScore.add(10005L);

        // 保存到文件
        objectMapper.writeValue(new File(basePath + "/sPositionsScore.json"), sPositionsScore);

        // 生成按工序查找的分数表数据
        Map<Long, List<Score>> hScoreByProcess = new HashMap<>();

        // 工序1的分数
        List<Score> scores1 = new ArrayList<>();
        Score score1_1 = new Score();
        score1_1.setPositionId(10001L);
        score1_1.setTotal(80.0);
        double[] scoreItems1 = new double[20];
        score1_1.setScoreItem(scoreItems1);
        scores1.add(score1_1);

        Score score1_2 = new Score();
        score1_2.setPositionId(10002L);
        score1_2.setTotal(75.0);
        double[] scoreItems2 = new double[20];
        score1_2.setScoreItem(scoreItems2);
        scores1.add(score1_2);

        hScoreByProcess.put(1001L, scores1);

        // 工序2的分数
        List<Score> scores2 = new ArrayList<>();
        Score score2_1 = new Score();
        score2_1.setPositionId(10002L);
        score2_1.setTotal(85.0);
        double[] scoreItems3 = new double[20];
        score2_1.setScoreItem(scoreItems3);
        scores2.add(score2_1);

        Score score2_2 = new Score();
        score2_2.setPositionId(10003L);
        score2_2.setTotal(80.0);
        double[] scoreItems4 = new double[20];
        score2_2.setScoreItem(scoreItems4);
        scores2.add(score2_2);

        hScoreByProcess.put(1002L, scores2);

        // 工序3的分数 (挂起状态)
        List<Score> scores3 = new ArrayList<>();
        Score score3_1 = new Score();
        score3_1.setPositionId(10004L);
        score3_1.setTotal(70.0);
        double[] scoreItems5 = new double[20];
        score3_1.setScoreItem(scoreItems5);
        scores3.add(score3_1);

        hScoreByProcess.put(1003L, scores3);

        // 工序4的分数 (NG状态)
        List<Score> scores4 = new ArrayList<>();
        Score score4_1 = new Score();
        score4_1.setPositionId(10005L);
        score4_1.setTotal(90.0);
        double[] scoreItems6 = new double[20];
        score4_1.setScoreItem(scoreItems6);
        scores4.add(score4_1);

        hScoreByProcess.put(1004L, scores4);

        // 保存到文件
        objectMapper.writeValue(new File(basePath + "/hScoreByProcess.json"), hScoreByProcess);

        // 生成函数参数，ratio值大于0.01
        Map<String, Object> functionParams = new HashMap<>();
        functionParams.put("ratio", 0.1);
        objectMapper.writeValue(new File(basePath + "/testCases.json"), functionParams);
    }

    /**
     * 生成手动测试案例2 - 包含各种加急情况
     */
    private static void generateManualTestCase2(String basePath) throws IOException {
        // 生成订单数据
        Map<String, OrderDto> orderId2Order = new HashMap<>();

        // 订单1 (加急订单)
        OrderDto order1 = OrderDto.InitOrder();
        order1.setMLocalOrderNum("ORDER003");
        order1.setMColor("绿色");
        order1.setMLineColorNum("LINE003");
        order1.setMOuterOrderNum("OUTER003");
        order1.mbPriFlag = true; // 加急订单
        orderId2Order.put(order1.getMLocalOrderNum(), order1);

        // 保存到文件
        objectMapper.writeValue(new File(basePath + "/orderId2Order.json"), orderId2Order);

        // 生成工序加急等级数据
        Map<String, Float> processUrgentLevels = new HashMap<>();
        processUrgentLevels.put("HIGH", 100.0f);
        processUrgentLevels.put("MEDIUM", 50.0f);
        processUrgentLevels.put("LOW", 20.0f);
        processUrgentLevels.put("DEFAULT", 10.0f);
        objectMapper.writeValue(new File(basePath + "/processUrgentLevels.json"), processUrgentLevels);

        // 生成工序集数据
        Map<Long, OrderProcess> hProcessSet = new HashMap<>();

        // 工序1 (高加急等级工序)
        OrderProcess process1 = OrderProcess.InitOrderProcess(order1);
        process1.setProcessId(2001L);
        process1.setOrderProcessId(2001L);

        ProcessSch scheduling1 = new ProcessSch();
        scheduling1.setMbHoldFlag(false);
        scheduling1.setMbNGFlag(false);
        scheduling1.setMbUrgentFlag(true); // 加急工序
        scheduling1.setMbUrgentCode("HIGH"); // 高等级加急

        // 设置可做工位
        Set<Long> posCanDo1 = new HashSet<>();
        posCanDo1.add(20001L);
        posCanDo1.add(20002L);
        posCanDo1.add(20003L);
        scheduling1.setMPosCanDo(posCanDo1);

        process1.setMScheduling(scheduling1);
        hProcessSet.put(2001L, process1);

        // 工序2 (中等加急等级工序)
        OrderProcess process2 = OrderProcess.InitOrderProcess(order1);
        process2.setProcessId(2002L);
        process2.setOrderProcessId(2002L);

        ProcessSch scheduling2 = new ProcessSch();
        scheduling2.setMbHoldFlag(false);
        scheduling2.setMbNGFlag(false);
        scheduling2.setMbUrgentFlag(true); // 加急工序
        scheduling2.setMbUrgentCode("MEDIUM"); // 中等级加急

        // 设置可做工位
        Set<Long> posCanDo2 = new HashSet<>();
        posCanDo2.add(20003L);
        posCanDo2.add(20004L);
        scheduling2.setMPosCanDo(posCanDo2);

        process2.setMScheduling(scheduling2);
        hProcessSet.put(2002L, process2);

        // 工序3 (低加急等级工序)
        OrderProcess process3 = OrderProcess.InitOrderProcess(order1);
        process3.setProcessId(2003L);
        process3.setOrderProcessId(2003L);

        ProcessSch scheduling3 = new ProcessSch();
        scheduling3.setMbHoldFlag(false);
        scheduling3.setMbNGFlag(false);
        scheduling3.setMbUrgentFlag(true); // 加急工序
        scheduling3.setMbUrgentCode("LOW"); // 低等级加急

        // 设置可做工位
        Set<Long> posCanDo3 = new HashSet<>();
        posCanDo3.add(20004L);
        posCanDo3.add(20005L);
        scheduling3.setMPosCanDo(posCanDo3);

        process3.setMScheduling(scheduling3);
        hProcessSet.put(2003L, process3);

        // 工序4 (未知加急等级工序)
        OrderProcess process4 = OrderProcess.InitOrderProcess(order1);
        process4.setProcessId(2004L);
        process4.setOrderProcessId(2004L);

        ProcessSch scheduling4 = new ProcessSch();
        scheduling4.setMbHoldFlag(false);
        scheduling4.setMbNGFlag(false);
        scheduling4.setMbUrgentFlag(true); // 加急工序
        scheduling4.setMbUrgentCode("UNKNOWN"); // 未知加急等级

        // 设置可做工位
        Set<Long> posCanDo4 = new HashSet<>();
        posCanDo4.add(20005L);
        posCanDo4.add(20006L);
        scheduling4.setMPosCanDo(posCanDo4);

        process4.setMScheduling(scheduling4);
        hProcessSet.put(2004L, process4);

        // 工序5 (无加急代码工序)
        OrderProcess process5 = OrderProcess.InitOrderProcess(order1);
        process5.setProcessId(2005L);
        process5.setOrderProcessId(2005L);

        ProcessSch scheduling5 = new ProcessSch();
        scheduling5.setMbHoldFlag(false);
        scheduling5.setMbNGFlag(false);
        scheduling5.setMbUrgentFlag(true); // 加急工序
        scheduling5.setMbUrgentCode(""); // 无加急代码

        // 设置可做工位
        Set<Long> posCanDo5 = new HashSet<>();
        posCanDo5.add(20006L);
        posCanDo5.add(20007L);
        scheduling5.setMPosCanDo(posCanDo5);

        process5.setMScheduling(scheduling5);
        hProcessSet.put(2005L, process5);

        // 保存到文件
        objectMapper.writeValue(new File(basePath + "/hProcessSet.json"), hProcessSet);

        // 生成可以排产的工位数据
        Set<Long> sPositionsScore = new HashSet<>();
        sPositionsScore.add(20001L);
        sPositionsScore.add(20002L);
        sPositionsScore.add(20003L);
        sPositionsScore.add(20004L);
        sPositionsScore.add(20005L);
        sPositionsScore.add(20006L);
        sPositionsScore.add(20007L);

        // 保存到文件
        objectMapper.writeValue(new File(basePath + "/sPositionsScore.json"), sPositionsScore);

        // 生成按工序查找的分数表数据
        Map<Long, List<Score>> hScoreByProcess = new HashMap<>();

        // 工序1的分数 (高加急等级)
        List<Score> scores1 = new ArrayList<>();
        Score score1_1 = new Score();
        score1_1.setPositionId(20001L);
        score1_1.setTotal(95.0);
        double[] scoreItems1 = new double[20];
        score1_1.setScoreItem(scoreItems1);
        scores1.add(score1_1);

        Score score1_2 = new Score();
        score1_2.setPositionId(20002L);
        score1_2.setTotal(90.0);
        double[] scoreItems2 = new double[20];
        score1_2.setScoreItem(scoreItems2);
        scores1.add(score1_2);

        Score score1_3 = new Score();
        score1_3.setPositionId(20003L);
        score1_3.setTotal(85.0);
        double[] scoreItems3 = new double[20];
        score1_3.setScoreItem(scoreItems3);
        scores1.add(score1_3);

        hScoreByProcess.put(2001L, scores1);

        // 工序2的分数 (中等加急等级)
        List<Score> scores2 = new ArrayList<>();
        Score score2_1 = new Score();
        score2_1.setPositionId(20003L);
        score2_1.setTotal(80.0);
        double[] scoreItems4 = new double[20];
        score2_1.setScoreItem(scoreItems4);
        scores2.add(score2_1);

        Score score2_2 = new Score();
        score2_2.setPositionId(20004L);
        score2_2.setTotal(75.0);
        double[] scoreItems5 = new double[20];
        score2_2.setScoreItem(scoreItems5);
        scores2.add(score2_2);

        hScoreByProcess.put(2002L, scores2);

        // 工序3的分数 (低加急等级)
        List<Score> scores3 = new ArrayList<>();
        Score score3_1 = new Score();
        score3_1.setPositionId(20004L);
        score3_1.setTotal(70.0);
        double[] scoreItems6 = new double[20];
        score3_1.setScoreItem(scoreItems6);
        scores3.add(score3_1);

        Score score3_2 = new Score();
        score3_2.setPositionId(20005L);
        score3_2.setTotal(65.0);
        double[] scoreItems7 = new double[20];
        score3_2.setScoreItem(scoreItems7);
        scores3.add(score3_2);

        hScoreByProcess.put(2003L, scores3);

        // 工序4的分数 (未知加急等级)
        List<Score> scores4 = new ArrayList<>();
        Score score4_1 = new Score();
        score4_1.setPositionId(20005L);
        score4_1.setTotal(60.0);
        double[] scoreItems8 = new double[20];
        score4_1.setScoreItem(scoreItems8);
        scores4.add(score4_1);

        Score score4_2 = new Score();
        score4_2.setPositionId(20006L);
        score4_2.setTotal(55.0);
        double[] scoreItems9 = new double[20];
        score4_2.setScoreItem(scoreItems9);
        scores4.add(score4_2);

        hScoreByProcess.put(2004L, scores4);

        // 工序5的分数 (无加急代码)
        List<Score> scores5 = new ArrayList<>();
        Score score5_1 = new Score();
        score5_1.setPositionId(20006L);
        score5_1.setTotal(50.0);
        double[] scoreItems10 = new double[20];
        score5_1.setScoreItem(scoreItems10);
        scores5.add(score5_1);

        Score score5_2 = new Score();
        score5_2.setPositionId(20007L);
        score5_2.setTotal(45.0);
        double[] scoreItems11 = new double[20];
        score5_2.setScoreItem(scoreItems11);
        scores5.add(score5_2);

        hScoreByProcess.put(2005L, scores5);

        // 保存到文件
        objectMapper.writeValue(new File(basePath + "/hScoreByProcess.json"), hScoreByProcess);

        // 生成函数参数
        Map<String, Object> functionParams = new HashMap<>();
        functionParams.put("ratio", 0.15);
        objectMapper.writeValue(new File(basePath + "/testCases.json"), functionParams);
    }

    /**
     * 清空指定目录下的所有文件
     *
     * @param directory 要清空的目录
     * @throws IOException IO异常
     */
    private static void clearDirectory(File directory) throws IOException {
        if (directory.exists() && directory.isDirectory()) {
            File[] files = directory.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (file.isFile()) {
                        Files.delete(file.toPath());
                    }
                }
            }
        }
    }

    /**
     * 生成订单映射表数据
     */
    private static Map<String, OrderDto> generateOrderId2OrderData(String basePath) throws IOException {
        Map<String, OrderDto> orderId2Order = new HashMap<>();

        // 随机生成订单数量 2-5个
        int orderCount = 2 + random.nextInt(4);

        for (int i = 0; i < orderCount; i++) {
            OrderDto order = OrderDto.InitOrder();
            String orderNum = "ORDER" + (1000 + i);
            order.setMLocalOrderNum(orderNum);
            order.setMColor("颜色" + i);
            order.setMLineColorNum("LINE" + (100 + i));
            order.setMOuterOrderNum("OUTER" + (1000 + i));
            order.mbPriFlag = random.nextBoolean(); // 随机设置是否为加急订单

            orderId2Order.put(orderNum, order);
        }

        // 保存到文件
        objectMapper.writeValue(new File(basePath + "/orderId2Order.json"), orderId2Order);

        return orderId2Order;
    }

    /**
     * 生成工序加急等级映射数据
     */
    private static void generateProcessUrgentLevelsData(String basePath) throws IOException {
        Map<String, Float> processUrgentLevels = new HashMap<>();

        // 定义几种加急等级
        processUrgentLevels.put("URGENT", 100.0f);
        processUrgentLevels.put("HIGH_PRIORITY", 80.0f);
        processUrgentLevels.put("MEDIUM_PRIORITY", 50.0f);
        processUrgentLevels.put("LOW_PRIORITY", 20.0f);

        // 保存到文件
        objectMapper.writeValue(new File(basePath + "/processUrgentLevels.json"), processUrgentLevels);
    }

    /**
     * 生成工序集数据
     */
    private static Map<Long, OrderProcess> generateProcessSetData(String basePath, 
                                                                 Map<String, OrderDto> orderId2Order) throws IOException {
        Map<Long, OrderProcess> hProcessSet = new HashMap<>();

        // 获取订单列表
        List<String> orderNums = new ArrayList<>(orderId2Order.keySet());
        if (orderNums.isEmpty()) {
            // 如果没有订单，创建一个默认订单
            OrderDto defaultOrder = OrderDto.InitOrder();
            defaultOrder.setMLocalOrderNum("DEFAULT_ORDER");
            orderId2Order.put("DEFAULT_ORDER", defaultOrder);
            orderNums.add("DEFAULT_ORDER");
        }

        // 随机生成工序数量 3-8个
        int processCount = 3 + random.nextInt(6);

        List<String> orderNumList = new ArrayList<>(orderId2Order.keySet());

        for (int i = 0; i < processCount; i++) {
            String orderNum = orderNumList.get(random.nextInt(orderNumList.size()));
            OrderDto orderDto = orderId2Order.get(orderNum);

            OrderProcess process = OrderProcess.InitOrderProcess(orderDto);
            long processId = 1000L + i * 10 + random.nextInt(10);
            process.setProcessId(processId);
            process.setOrderProcessId(processId);

            ProcessSch scheduling = new ProcessSch();
            scheduling.setMbHoldFlag(random.nextDouble() < 0.1); // 10%概率为挂起状态
            scheduling.setMbNGFlag(random.nextDouble() < 0.1); // 10%概率为NG状态
            scheduling.setMbUrgentFlag(random.nextDouble() < 0.3); // 30%概率为加急工序

            // 随机设置加急代码
            if (scheduling.isMbUrgentFlag()) {
                String[] urgentCodes = {"URGENT", "HIGH_PRIORITY", "MEDIUM_PRIORITY", "LOW_PRIORITY"};
                scheduling.setMbUrgentCode(urgentCodes[random.nextInt(urgentCodes.length)]);
            } else {
                scheduling.setMbUrgentCode("");
            }

            // 设置可做工位集合
            Set<Long> posCanDo = new HashSet<>();
            int posCount = 2 + random.nextInt(5); // 2-6个工位
            for (int j = 0; j < posCount; j++) {
                posCanDo.add((long) (10000 + random.nextInt(90000))); // 10000-99999的工位ID
            }
            scheduling.setMPosCanDo(posCanDo);

            process.setMScheduling(scheduling);
            hProcessSet.put(processId, process);
        }

        // 保存到文件
        objectMapper.writeValue(new File(basePath + "/hProcessSet.json"), hProcessSet);

        return hProcessSet;
    }

    /**
     * 生成可以排产的工位数据
     */
    private static Set<Long> generatePositionScoreData(String basePath) throws IOException {
        Set<Long> sPositionsScore = new HashSet<>();

        // 随机生成工位数量 5-15个
        int positionCount = 5 + random.nextInt(11);

        // 添加测试工位ID
        for (int i = 0; i < positionCount; i++) {
            sPositionsScore.add((long) (10000 + random.nextInt(90000))); // 10000-99999的工位ID
        }

        // 保存到文件
        objectMapper.writeValue(new File(basePath + "/sPositionsScore.json"), sPositionsScore);

        return sPositionsScore;
    }

    /**
     * 生成按工序查找的分数表数据
     */
    private static void generateScoreByProcessData(String basePath, Map<Long, OrderProcess> hProcessSet,
                                                   Set<Long> sPositionsScore) throws IOException {
        Map<Long, List<Score>> hScoreByProcess = new HashMap<>();

        // 使用已生成的工序ID来创建分数数据
        List<Long> processIds = new ArrayList<>(hProcessSet.keySet());

        for (Long processId : processIds) {
            List<Score> scores = new ArrayList<>();

            // 获取部分工位ID用于这个工序
            List<Long> positionIds = new ArrayList<>(sPositionsScore);
            Collections.shuffle(positionIds); // 随机打乱

            // 每个工序对应1-5个工位分数（不超过工位总数）
            int scoreCount = Math.min(1 + random.nextInt(5), positionIds.size());
            for (int j = 0; j < scoreCount; j++) {
                Score score = new Score();
                score.setPositionId(positionIds.get(j));
                // 总分保留两位小数
                double total = random.nextDouble() * 100; // 0-100的总分
                total = new BigDecimal(total).setScale(2, RoundingMode.HALF_UP).doubleValue();
                score.setTotal(total);

                // 初始化分数项数组，包含随机分数，每项保留两位小数
                double[] scoreItems = new double[20];
                for (int k = 0; k < scoreItems.length; k++) {
                    double item = random.nextDouble() * 50; // 0-50的分数项
                    item = new BigDecimal(item).setScale(2, RoundingMode.HALF_UP).doubleValue();
                    scoreItems[k] = item;
                }
                score.setScoreItem(scoreItems);

                scores.add(score);
            }

            hScoreByProcess.put(processId, scores);
        }

        // 保存到文件
        objectMapper.writeValue(new File(basePath + "/hScoreByProcess.json"), hScoreByProcess);
    }

    /**
     * 生成函数参数
     */
    private static void generateFunctionParams(String basePath) throws IOException {
        Map<String, Object> functionParams = new HashMap<>();
        // 生成 0.01 到 1.0 之间的随机 ratio 值，保留两位小数
        double randomRatio = 0.01 + random.nextDouble() * 0.99;
        randomRatio = new BigDecimal(randomRatio).setScale(2, RoundingMode.HALF_UP).doubleValue();
        functionParams.put("ratio", randomRatio);
        objectMapper.writeValue(new File(basePath + "/testCases.json"), functionParams);
    }

    public static void main(String[] args) {
        try {
            // 确保主目录存在
            File mainDir = new File("../../test_data/calculateItem_7");
            if (!mainDir.exists()) {
                mainDir.mkdirs();
            }

            // 生成50组随机测试数据
            for (int i = 1; i <= 50; i++) {
                generateRandomTestData(i);
                System.out.println("随机测试数据集 " + i + " 已成功生成");
            }

            // 生成2个手动测试案例
            for (int i = 1; i <= 2; i++) {
                generateManualTestData(i);
                System.out.println("手动测试案例 " + i + " 已成功生成");
            }

            System.out.println("所有测试数据已成功生成并保存到../../test_data/calculateItem_7目录下");
        } catch (IOException e) {
            System.err.println("生成测试数据时发生错误: " + e.getMessage());
            e.printStackTrace();
        }
    }
}